package view.admin;

import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPasswordField;

import controller.ActionEvents.ActionHandler;
import javax.swing.ImageIcon;


public class AdminLogin extends JPanel {
	private JLabel lblCCCS;
	private JLabel lblSmallHeader;
	private JTextField txtAdmin;
	private JButton btnAdminLogin;
	private JPasswordField passwordFieldAdmin;
	private JButton btnBack;
	private ActionHandler actionHandler;

	/**
	 * Create the panel.
	 * @param adminController 
	 */
	public AdminLogin(ActionHandler actionHandler) {
		setPreferredSize(new Dimension(750, 480));
		setLayout(null);
		setBackground(Color.WHITE);
		
		this.actionHandler = actionHandler;
		
		lblCCCS = new JLabel("Carbon Credits System");
		lblCCCS.setFont(new Font("Euphemia UCAS", Font.PLAIN, 28));
		lblCCCS.setBounds(239, 68, 307, 53);
		add(lblCCCS);
		
		lblSmallHeader = new JLabel("Admin logon");
		lblSmallHeader.setFont(new Font("Euphemia UCAS", Font.PLAIN, 14));
		lblSmallHeader.setBounds(349, 131, 180, 16);
		add(lblSmallHeader);
		
		txtAdmin = new JTextField();
		txtAdmin.setText("170291");
		txtAdmin.setColumns(10);
		txtAdmin.setBackground(Color.decode("#f1f1f1"));
		txtAdmin.setBounds(320, 159, 142, 28);
		add(txtAdmin);
		
		btnAdminLogin = new JButton("");
		btnAdminLogin.setIcon(new ImageIcon(AdminLogin.class.getResource("/Pictures/ux/submit.png")));
		btnAdminLogin.addActionListener(actionHandler); //Passes event to actionPerformed
		btnAdminLogin.setActionCommand("showAdminLogin");
		btnAdminLogin.setBorderPainted(false);
		btnAdminLogin.setContentAreaFilled(false);
		btnAdminLogin.setBounds(330, 231, 142, 47);
		add(btnAdminLogin);
		
		passwordFieldAdmin = new JPasswordField();
		passwordFieldAdmin.setToolTipText("Password");
		passwordFieldAdmin.setText("hej");
		passwordFieldAdmin.setBackground(Color.decode("#f1f1f1"));
		passwordFieldAdmin.setBounds(320, 192, 142, 28);
		KeyListener kl;
		passwordFieldAdmin.addKeyListener(kl = new KeyListener(){

			@Override
			public void keyPressed(KeyEvent e) {}
			@Override
			
			public void keyReleased(KeyEvent e) {
				int key = e.getKeyCode();
				if(key == KeyEvent.VK_ENTER)
				{
					btnAdminLogin.doClick();
					txtAdmin.requestFocusInWindow(); /* Sets the focus to the textfield. If this werent done, the keylistener would
					loop infinite as long as the user tried to use the ENTER key to press the OptionPane. */
				}
			}
			@Override
			public void keyTyped(KeyEvent e) {}
			
		});
		add(passwordFieldAdmin);
		
		btnBack = new JButton("");
		btnBack.setIcon(new ImageIcon(AdminLogin.class.getResource("/Pictures/ux/back.png")));
		btnBack.setBounds(599, 431, 134, 29);
		btnBack.addActionListener(actionHandler);
		btnBack.setBorderPainted(false);
		btnBack.setContentAreaFilled(false);
		btnBack.setActionCommand("backFromAdmin");
		add(btnBack);

	}

	/**
	 * @return the textField
	 */
	public JTextField getTxtAdmin() {
		return txtAdmin;
	}

	/**
	 * @param txtAdmin the textField to set
	 */
	public void setTxtAdmin(JTextField txtAdmin) {
		this.txtAdmin = txtAdmin;
	}

	/**
	 * @return the passwordFieldAdmin
	 */
	public JPasswordField getPasswordFieldAdmin() {
		return passwordFieldAdmin; 
	}

	/**
	 * @param passwordFieldAdmin the passwordFieldAdmin to set  
	 */
	public void setPasswordFieldAdmin(JPasswordField passwordFieldAdmin) {
		this.passwordFieldAdmin = passwordFieldAdmin;
	}

	/**
	 * @return the btnAdminLogin
	 */
	public JButton getBtnAdminLogin() {
		return btnAdminLogin;
	}

	/**
	 * @return the btnBack
	 */
	public JButton getBtnBack() {
		return btnBack;
	}
	
}
